#!/bin/bash

# use vita to execute these commands
unset LANG
export MYROOT=/home/andy/work/linux_vita
export HOST=i686-pc-linux-gnu
export BUILD=$HOST
export TARGET=i686-none-linux-gnu
export CROSS_TOOL=$MYROOT/cross-tool
export CROSS_GCC_TMP=$MYROOT/cross-gcc-tmp
export SYSROOT=$MYROOT/sysroot
export PATH=$CROSS_TOOL/bin:$CROSS_GCC_TMP/bin:/sbin:/usr/sbin:$PATH

SRC_DIR=$MYROOT/source
BUILD_DIR=$MYROOT/build

BINUTILS_VERSION="2.23.1"
GCC_VERSION="4.7.2"
GMP_VERSION="5.0.5"
MPFR_VERSION="3.1.1"
MPC_VERSION="1.0.1"
GLIBC_VERSION="2.15"
KERNEL_VERSION="3.7.4"

echo "P45 2.2.4 install binutils"
cd $BUILD_DIR
tar -xv -f $SRC_DIR/binutils-${BINUTILS_VERSION}.tar.bz2
mkdir binutils-build
cd binutils-build/
../binutils-${BINUTILS_VERSION}/configure --prefix=$CROSS_TOOL --target=$TARGET --with-sysroot=$SYSROOT --enable-obsolete
make
make install
cd ..

echo "P47 2.2.5 install gcc"
cd $BUILD_DIR
tar -xv -f $SRC_DIR/gcc-${GCC_VERSION}.tar.bz2
cd gcc-${GCC_VERSION}/
tar -xv -f $SRC_DIR/gmp-${GMP_VERSION}.tar.bz2
mv gmp-${GMP_VERSION}/ gmp
tar -xv -f $SRC_DIR/mpfr-${MPFR_VERSION}.tar.bz2
mv mpfr-${MPFR_VERSION}/ mpfr
tar -xv -f $SRC_DIR/mpc-${MPC_VERSION}.tar.gz
mv mpc-${MPC_VERSION}/ mpc
cd ..
mkdir gcc-build
cd gcc-build
../gcc-${GCC_VERSION}/configure --prefix=$CROSS_GCC_TMP --target=$TARGET --with-sysroot=$SYSROOT --with-newlib --enable-languages=c --with-mpfr-include=$BUILD_DIR/gcc-${GCC_VERSION}/mpfr/src --with-mpfr-lib=$BUILD_DIR/gcc-build/mpfr/src/.libs --disable-shared --disable-threads --disable-decimal-float --disable-libquadmath --disable-libmudflap --disable-libgomp --disable-nls --disable-libssp
make
make install
cd ..
mv gcc-build gcc-build-01
mv gcc-${GCC_VERSION} gcc-${GCC_VERSION}-01
cd $CROSS_GCC_TMP
ln -s libgcc.a lib/gcc/$TARGET/${GCC_VERSION}/libgcc_eh.a


echo "P49 2.2.6 install kernel header files"
cd $BUILD_DIR
tar -xv -f $SRC_DIR/linux-${KERNEL_VERSION}.tar.xz
cd linux-${KERNEL_VERSION}/
make mrproper
make ARCH=i386 headers_check
make ARCH=i386 INSTALL_HDR_PATH=$SYSROOT/usr/ headers_install
cd ..


echo "P50 2.2.7 compile C libraries of target system"
cd $BUILD_DIR
tar -xv -f $SRC_DIR/glibc-${GLIBC_VERSION}.tar.xz
cd glibc-${GLIBC_VERSION}/
patch -p1 < $SRC_DIR/glibc-${GLIBC_VERSION}-cpuid.patch
patch -p1 < $SRC_DIR/glibc-${GLIBC_VERSION}-s_frexp.patch
cd ..
mkdir glibc-build
cd glibc-build/
../glibc-${GLIBC_VERSION}/configure --prefix=/usr --host=$TARGET --enable-kernel=${KERNEL_VERSION} --enable-add-ons --with-headers=$SYSROOT/usr/include libc_cv_forced_unwind=yes libc_cv_c_cleanup=yes libc_cv_ctors_header=yes
make
make install_root=$SYSROOT install
cd ..

echo "P52 2.2.8 compile complete compiler"
cd $BUILD_DIR
tar -xv -f $SRC_DIR/gcc-${GCC_VERSION}.tar.bz2
cd gcc-${GCC_VERSION}/
tar -xv -f $SRC_DIR/gmp-${GMP_VERSION}.tar.bz2
mv gmp-${GMP_VERSION}/ gmp
tar -xv -f $SRC_DIR/mpfr-${MPFR_VERSION}.tar.bz2
mv mpfr-${MPFR_VERSION}/ mpfr
tar -xv -f $SRC_DIR/mpc-${MPC_VERSION}.tar.gz
mv mpc-${MPC_VERSION}/ mpc
cd ..
mkdir gcc-build
cd gcc-build
../gcc-${GCC_VERSION}/configure --prefix=$CROSS_TOOL --target=$TARGET --with-sysroot=$SYSROOT --with-mpfr-include=$BUILD_DIR/gcc-${GCC_VERSION}/mpfr/src --with-mpfr-lib=$BUILD_DIR/gcc-build/mpfr/src/.libs --enable-languages=c,c++ --enable-threads=posix
make
make install
cd ..

echo "build toolchain complete!"



